<?php

class Admin_GrandprixController extends Zend_Controller_Action
{

    public function init()
    {
		if(!Zend_Auth::getInstance()->hasIdentity())  
		{
			$this->_redirect('admin/login');  
		}  
    }

    public function indexAction()
    {
    }
    
    public function addAction()
    {
    	$form = new Application_Form_Grandprix();
    	$grandprixDAO = new Application_Model_GrandprixDAO();
    	$circuitDAO = new Application_Model_CircuitDAO();
		$this->view->form = $form;
		if($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData))
			{
				$values = $form->getValues();
				$grandprix = new Application_Model_Grandprix();
				$grandprix->setGpNom($values['gpnom']);
				$grandprix->setCircuit($circuitDAO->find($values['cirnum']));
				$grandprix->setGpDate($values['gpdate']);
				$grandprix->setGpNbTours($values['gpnbtours']);
				$grandprix->setGpCommentaire($values['gpcommentaire']);

				if($grandprixDAO->save($grandprix))
				{
					$this->_redirect('admin/grandprix');
				}
				else
				{
					$form->populate($values);
				}
			}
			else
			{
				$form->populate($form->getValues());
			}
		}
    }
    
    public function updateAction()
    {
    	$grandprixDAO = new Application_Model_GrandprixDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	        $form = new Application_Form_Grandprix();
	    	$circuitDAO = new Application_Model_CircuitDAO();
	    	$grandprix = $grandprixDAO->find($id);
	    	if(!is_null($grandprix))
	    	{
				$this->view->form = $form;
				$form->populate($grandprix->toArray());
				if($this->getRequest()->isPost())
				{
					$formData = $this->getRequest()->getPost();
					if($form->isValid($formData))
					{
						$values = $form->getValues();
						$grandprix = new Application_Model_Grandprix();
						$grandprix->setGpNum($values['gpnum']);
						$grandprix->setGpNom($values['gpnom']);
						$grandprix->setCircuit($circuitDAO->find($values['cirnum']));
						$grandprix->setGpDate($values['gpdate']);
						$grandprix->setGpNbTours($values['gpnbtours']);
						$grandprix->setGpCommentaire($values['gpcommentaire']);
		
						if($grandprixDAO->save($grandprix))
						{
							$this->_redirect('admin/grandprix');
						}
						else
						{
							$form->populate($values);
						}
					}
					else
					{
						$form->populate($form->getValues());
					}
				}
			}
			else
			{
				$this->_redirect('admin/grandprix');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->grandprix = $grandprixDAO->findAll();
    	}
    }
	
    public function deleteAction()
    {
        $grandprixDAO = new Application_Model_GrandprixDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	    	$grandprix = $grandprixDAO->find($id);
	    	if(!is_null($grandprix))
	    	{
				$grandprixDAO->delete($grandprix->getGpNum());
				$this->_redirect('admin/grandprix');
			}
			else
			{
				$this->_redirect('admin/grandprix');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->grandprix = $grandprixDAO->findAll();
    	}    	
    }
}